package com.nidu.demo.notice.executor.command;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.notice.convertor.NoticeConvertor;
import com.nidu.demo.notice.dto.NoticeUpdateCmd;
import com.nidu.demo.notice.dto.NoticeCO;
import com.nidu.demo.notice.gateway.NoticeGateway;
import com.nidu.demo.notice.model.Notice;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知公告更新命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class NoticeUpdateCmdExe {

    private final NoticeConvertor convertor;
    private final NoticeGateway gateway;

    @Transactional
    public SingleResponse<NoticeCO> execute(NoticeUpdateCmd cmd) {

        // 1. 更新通知公告信息
        Notice notice = Notice.update(cmd.getId(), cmd.getTitle(), cmd.getContent(), cmd.getType());

        // 2. 保存更新
        Notice updated = gateway.update(notice);

        // 3. 转换为DTO返回
        NoticeCO noticeCO = convertor.toClientObject(updated);
        return SingleResponse.of(noticeCO);
    }

    @Transactional
    public Response updateStatus(Long id, StatusEnum status) {
        gateway.updateStatus(id, status.getValue());
        return Response.buildSuccess();
    }

}
